package com.gf.framework.web.translate;

import com.gf.framework.common.model.IDict;

import java.util.function.Function;
import java.util.stream.Stream;

public class EnumTranslatable implements Translatable<Object, String> {

    @Override
    @SuppressWarnings("unchecked")
    public String translate(Object original, Class<?> datasource, String param) {
        Class<IDict<?, ?>> iDict = (Class<IDict<?, ?>>) datasource;
        return Stream.of(iDict.getEnumConstants())
                .filter(i -> i.getCode().equals(original)).map(IDict::getValue).map((Function<Object, String>) Object::toString).findFirst().orElse(null);
    }
}
